$(function() {
	 var userInfo = $.cookie('login_cookie');
     if (userInfo != null && userInfo != "") {
			var name = userInfo.split("%")[1];
			$(".header-left").html(
					"欢迎，"+name+"&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' class='link-logout' onclick='logout()'>[退出]</a>"		
			);
		} else {
			window.location.href = ("login.html");
		}
	var f=true;
	var p=true;
	//注册信息验证
	$("input[name='name'],input[name='teacher'],input[name='schedule'],input[name='phone'],input[name='age'],input[name='addr']").blur(function(){
		var flag =true;
		var flag2=true;
		var phone=true;
		var name=$("input[name='name']").val();
		var id=$("input[name='id']").val();
		var phone=$("input[name='phone']").val();		
		var teacher=$("input[name='teacher']").val();
		var schedule=$("input[name='schedule']").val();
		var age=$("input[name='age']").val();
		var addr=$("input[name='addr']").val();
		var gender=$("input[name='gender']:checked").val();
		if(name==""){
			flag=flag && false;
		}
		if(phone==""){
			flag=flag && false;
		}
		if(id==""){
			flag=flag && false;
		}
		if(age==""){
			flag=flag && false;
		}
		if(addr==""){
			flag=flag && false;
		}
		if(teacher=="")
			flag=flag && false;
			
		if(schedule==""){
			flag=flag && false;
		} 
		f=flag;
		if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(phone))){ 
			phone=phone && false;
			 }
		p=phone;
	});
	
	var search=window.location.search;
	var id=search.substring(1, search.length);
	$.ajax({
		url:"student/selectInfo",
		data:{"id":id},
		type:"post",
		dataType:"json",
		async:false,
		success:function(data){
			$("input[name='name']").val(data.name);
			$("input[name='id']").val(data.id);
			$("input[name='phone']").val(data.phone);		
			$("input[name='teacher']").val(data.teacher);
			$("input[name='schedule']").val(data.schedule);
			$("input[name='age']").val(data.age);
			$("input[name='addr']").val(data.addr);
			$("input[value='"+data.gender+"']").attr("checked","checked");
		}
		
	});
	//为注册按钮绑定点击注册时间  出发regist方法
	$("#payment-button").click(function(){
		if(!f){
			alert("学员信息不能为空！")
		}else if(!p){
			alert("不是是完整的11位手机号或者正确的手机号前七位")
		}else{ 
			alert("请求成功！")
			return update();
		}
	});
})
 function logout() {
		$.ajax({
			url : "user/logout",
			type : "post",
			dateType : "json",
			success : function(data) {
				window.location.reload();
			},
			error : function(e) {
				alert("请求失败！");
			}
		})
}
function update(){
	var search=window.location.search;
	var info=search.substring(1, search.length);
	var name=$("input[name='name']").val();
	var id=$("input[name='id']").val();
	var phone=$("input[name='phone']").val();		
	var teacher=$("input[name='teacher']").val();
	var schedule=$("input[name='schedule']").val();
	var age=$("input[name='age']").val();
	var addr=$("input[name='addr']").val();
	var gender=$("input[name='gender']:checked").val();
	
	$.ajax({
		url:"student/update",
		data:{"info":info,"name":name,"id":id,"teacher":teacher,"phone":phone,"schedule":schedule,"age":age,"addr":addr,"gender":gender},
		type:"post",
		dataType:"json",
		async:false,
		success:function(data){
			var flag=data.success;
			if(flag){
				alert("修改成功！");
				window.location.href="select-student.html";
			}
		},
		error:function(e){
			alert("修改失败！");
			window.location.href="add-student.html";
		}
	})
}